package cn.change.basic.controller;
import cn.change.basic.util.AjaxResult;
import cn.change.basic.util.FastdfsUtil;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @author 16252
 */
@RestController
@RequestMapping("/fastDfs")
public class FastDfsController {

    //文件上传接口
    @PostMapping
    public AjaxResult upload(@RequestPart(value = "file", required = true) MultipartFile file){
        try {
            String filename = file.getOriginalFilename();
            String extName = filename.substring(filename.lastIndexOf(".")+1);
            String resultPath = FastdfsUtil.upload(file.getBytes(), extName);
            return AjaxResult.me().setResultObj(resultPath);
        } catch (IOException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，稍后重试！！！！");
        }
    }
    //文件删除接口
    @DeleteMapping
    public AjaxResult delete(@RequestParam(value = "path",required = true) String path){
        try {
            String pathTmp = path.substring(1);
            String groupName =  pathTmp.substring(0, pathTmp.indexOf("/"));
            String remotePath = pathTmp.substring(pathTmp.indexOf("/")+1);
            FastdfsUtil.delete(groupName,remotePath);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，稍后重试！！！！");
        }
    }
}
